﻿using GalaSoft.MvvmLight.Messaging;
using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace HuiZe.education.View.Toolbar
{
    [AddINotifyPropertyChangedInterface]
    /// <summary>
    /// RandomAnswerShowView.xaml 的交互逻辑
    /// </summary>
    public partial class RandomAnswerShowView : Window
    {
        private GifImage gifImage;
        public RandomAnswerShowViewModel randomAnswer;
        private DispatcherTimer timer;
        public RandomAnswerShowView()
        {
            InitializeComponent();
            Loaded += Attendance_Loaded;
            TimeAutoStart();
        }

        public void TimeAutoStart()
        {
            if (timer == null)
            {
                timer = new DispatcherTimer();
            }
            timer.Interval = new TimeSpan(30000000);   //时间间隔为一秒
            timer.Tick += new EventHandler(timer_Tick);
        }
        private void timer_Tick(object sender, EventArgs e)
        {
           timer.Stop();
            UpdateRandImage();
        }
        private void Attendance_Loaded(object sender, RoutedEventArgs e)
        {

            randomAnswer = DataContext as RandomAnswerShowViewModel;
        }

        private bool isRandomCheck=false;
        private void RandomAnswerGrid_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            StarUp();
            //if (isRandomCheck)
            //    isRandomCheck = false;
            //else
            //    isRandomCheck = true;

            //if (isRandomCheck)
            //{
            //    randomAnswer.GetclassRandom();
            //}
          
        }
        private  void RandomAnswerGridOver_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            UpdateRandImage();
        }
        public void UpdateRandImage() {
            randomAnswer.GetclassRandom();
            System.Drawing.Bitmap bimage = new System.Drawing.Bitmap("Controls/image/go.png");
            try
            {
                this.gifImage = new GifImage("Controls/image/go.png");
                this.gifImage.Width = 304;
                this.gifImage.Height = 304;
                RandomAnswerGrid.Children.Add(gifImage);

                Messenger.Default.Send<int>(0, "updateImage");//更新中间图标
                Messenger.Default.Send<int>(2, "updateShowType");
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        public void StarUp()
        {
            this.gifImage = new GifImage("Controls/image/GIF.gif");
            this.gifImage.Width =304;
            this.gifImage.Height=304;
            this.RandomAnswerGrid.Children.Add(gifImage);

            this.gifImage.StartAnimate();
            this.gifImage.MouseLeftButtonUp += RandomAnswerGridOver_MouseLeftButtonUp;
            this.timer.Start();
        }

        /// <summary>
        /// 评论
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Messenger.Default.Send<int>(1, "updateImage");
            randomAnswer.classrandStastu();
        }

        private void YCButton_Click(object sender, RoutedEventArgs e)
        {
            this.gifImage = null;
            Messenger.Default.Send<int>(1, "updateShowType");
            StarUp();
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
    }
}
